﻿using System;
using System.Windows.Browser;
using System.Windows.Input;
using System.Windows.Interactivity;
using System.Windows.Shapes;

namespace FirstFloor.Documents.Presentation.Annotations
{
    /// <summary>
    /// Handles navigation on click of a hyperlink annotation.
    /// </summary>
    public class HyperlinkNavigationBehavior
        : Behavior<Rectangle>
    {
        /// <summary>
        /// Gets or sets the target URI.
        /// </summary>
        /// <value>The target URI.</value>
        public Uri TargetUri { get; set; }

        /// <summary>
        /// Called after the behavior is attached to an AssociatedObject.
        /// </summary>
        /// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
        protected override void OnAttached()
        {
            base.OnAttached();
            this.AssociatedObject.MouseLeftButtonDown += OnMouseLeftButtonDown;
        }

        /// <summary>
        /// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
        /// </summary>
        /// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.AssociatedObject.MouseLeftButtonDown -= OnMouseLeftButtonDown;
        }

        private void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // note: doesn't work in out-of-browser scenario's
            HtmlPage.PopupWindow(this.TargetUri, null, null);
        }
    }
}
